package cn.jiedh.app.meiweidk.msg;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.BindView;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.UI.CustomViewPager;
import cn.jiedh.app.meiweidk.Utils.UI.MyFragmentPagerAdapter;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.TabLayoutWidth;
import cn.jiedh.app.meiweidk.base.BaseActivity;

public class MsgActivity extends BaseActivity {
    MyFragmentPagerAdapter fragmentPagerAdapter;
    private ArrayList<Fragment> fragmentArrayList;

    private FragmentMsgInfo infoFragment;
    private FragmentMsgActi actiFragment;

    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    CustomViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_msg;
    }

    @Override
    protected void initView(View view) {
        title.setText(getResources().getString(R.string.msg_title));
        setBack(true);
        initPage();
    }

    @Override
    protected void initData() {
    }

    /**
     * 消息页面初始
     */
    private void initPage() {
        fragmentArrayList = new ArrayList<>();
        infoFragment = new FragmentMsgInfo();
        actiFragment = new FragmentMsgActi();
        fragmentArrayList.add(infoFragment);
        fragmentArrayList.add(actiFragment);
        fragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentArrayList);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOffscreenPageLimit(fragmentArrayList.size());
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.home_msg_info)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.home_msg_acti)));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (getResources().getString(R.string.home_msg_info).equals(tab.getText().toString())) {
                    MobclickAgent.onEvent(activity,"my_msg_click");
                    SPUtils.saveBuried("click","0","消息中心-通知提醒点击","",
                            "消息中心","","","");
                } else if (getResources().getString(R.string.home_msg_acti).equals(tab.getText().toString())) {
                    MobclickAgent.onEvent(activity,"my_msg_activity_click");
                    SPUtils.saveBuried("click","0","消息中心-优惠活动点击","",
                            "消息中心","","","");
                }
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                TabLayoutWidth.setIndicator(MsgActivity.this, tabLayout, 25, 25);
            }
        });
    }
}
